package com.xiaotao.test01.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

/**
 * @author xiaotao
 */
@Configuration
@EnableScheduling
@Slf4j
public class ScheduleConfig {

    @Bean("threadPoolTaskScheduler")
    public ThreadPoolTaskScheduler threadPoolTaskScheduler(){
        ThreadPoolTaskScheduler poolTaskScheduler = new ThreadPoolTaskScheduler();
        poolTaskScheduler.setPoolSize(10);
        poolTaskScheduler.setThreadNamePrefix("ScheduleThread-");
        poolTaskScheduler.setErrorHandler((e)->{
            e.printStackTrace();
            log.error("定时任务执行错误");
        });
        return poolTaskScheduler;
    }

    @Bean
    public ScheduledTaskRegistrar scheduledTaskRegistrar(){
        ScheduledTaskRegistrar scheduledTaskRegistrar = new ScheduledTaskRegistrar();
        scheduledTaskRegistrar.setScheduler(threadPoolTaskScheduler());
        return scheduledTaskRegistrar;
    }

}
